package com.tbit.uqbike.webmanager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.enums.OperaPswEnum;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.UserRidingScoreConfig;
import com.tbit.uqbike.object.pojo.UserRidingScoreLog;
import com.tbit.uqbike.object.pojo.dto.UserRidingScoreModifyDTO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.OperaPswService;
import com.tbit.uqbike.service.business.UserRidingScoreConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 用户骑行分
 *
 * @author yee_
 * @version 1.0
 * @date 2023/2/10 10:21
 */
@RestController
@RequestMapping("/riding/score")
public class UserRidingScoreController extends BaseController {

    @Resource
    private UserRidingScoreConfigService userRidingScoreService;
    @Autowired
    private OperaPswService operaPswService;

    /**
     * 获取品牌骑行分配置
     *
     * @param accountId 品牌id
     * @return 骑行分配置
     */
    @GetMapping("/config/{accountId}")
    public Result<UserRidingScoreConfig> findConfigByAccountId(@PathVariable("accountId") Integer accountId, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        roleAuthCheck(loginInfo, accountId);
        return userRidingScoreService.findConfigByAccountId(accountId);
    }

    /**
     * 修改品牌骑行分配置
     *
     * @param entity 配置信息
     * @return .
     */
    @PutMapping("/config/update")
    @OperateLog(module = ModuleEnum.RIDING_SCORE, methods = MethodEnum.RIDING_SCORE_EDIT)
    public Result<String> update(@RequestBody @Validated UserRidingScoreConfig entity, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        roleAuthCheck(loginInfo, entity.getAccountId());
        userRidingScoreService.update(entity);
        return Result.success();
    }

    /**
     * 新增品牌骑行分配置
     *
     * @param entity 配置信息
     * @return .
     */
    @PostMapping("/config/create")
    @OperateLog(module = ModuleEnum.RIDING_SCORE, methods = MethodEnum.RIDING_SCORE_ADD)
    public Result<String> create(@RequestBody @Validated UserRidingScoreConfig entity, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        roleAuthCheck(loginInfo, entity.getAccountId());
        userRidingScoreService.create(entity);
        return Result.success();
    }

    /**
     * 查询用户骑行分
     *
     * @param userId 用户id
     * @param page   页码
     * @param size   分页条数
     * @return 。
     */
    @GetMapping("/listByUserId")
    public Result<PageResult<UserRidingScoreLog>> getRidingScoreByUserId(@RequestParam Integer userId,
                                                                         @RequestParam(required = false, defaultValue = "1") Integer page,
                                                                         @RequestParam(required = false, defaultValue = "10") Integer size) {
        return userRidingScoreService.listRidingScoreLogByUserId(userId, Page.of(page, size));
    }

    /**
     * 获取用户骑行分
     */
    @GetMapping("/getByUserId/{userId}")
    public Result<Integer> getByUserId(@PathVariable("userId") Integer userId) {
        return userRidingScoreService.getByRidingScoreUserId(userId);
    }

    /**
     * 人工修改用户骑行分
     *
     * @param modifyDTO          参数entity
     * @param httpServletRequest 。
     * @return .
     */
    @PutMapping("/modifyUserRidingScore")
    public Result<String> modifyUserRidingScore(@RequestBody @Validated UserRidingScoreModifyDTO modifyDTO,
                                                HttpServletRequest httpServletRequest) {
        /**操作密码校验*/
        operaPswService.checkOperaPsw(BaseController.getLogin(httpServletRequest),
                    OperaPswEnum.OPERA_PSW_OPERATION_CONFIG.getModule(), modifyDTO.getPsw());

        LoginInfo login = getLogin(httpServletRequest);
        modifyDTO.setOperationId(login.getAccountUserId());
        modifyDTO.setOperationPhone(login.getPhone());
        return userRidingScoreService.modifyUserRidingScore(modifyDTO);
    }


    private void roleAuthCheck(LoginInfo login, Integer accountId) {
        if (!login.roleIsAdmin() && !Objects.equals(login.getId(), accountId)) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE, "");
        }
    }

}
